package com.dandelion.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dandelion.business.entity.SysMenu;
import com.dandelion.business.entity.SysUser;
import com.dandelion.business.entity.vo.user.CurrentUser;
import com.dandelion.business.entity.vo.user.req.QueryUserListReqVO;
import com.dandelion.business.entity.vo.user.req.UserReqVO;
import com.dandelion.business.mapper.SysUserMapper;
import com.dandelion.business.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dandelion.framework.common.R;
import com.dandelion.framework.enums.CodeEnum;
import com.dandelion.framework.enums.DelEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 宋发准
 * @since 2021-12-25
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 加载用户根据用户名
     *
     * @param username 用户名
     * @return {@link SysUser}
     */
    @Override
    public SysUser loadUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getUserName, username);
        return getOne(wrapper);
    }

    /**
     * 查询权限关键字
     *
     * @param id id
     * @return {@link ArrayList}<{@link String}>
     */
    @Override
    public ArrayList<SysMenu> queryPerms(Long id) {
        return userMapper.queryPerms(id);
    }

    /**
     * 添加
     *
     * @param currentUser  铜
     * @param req 要求事情
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> add(CurrentUser currentUser, UserReqVO req) {
        SysUser user = new SysUser();
        BeanUtil.copyProperties(req, user);
        user.setCreateBy(currentUser.getUserId());
        user.setUpdateBy(currentUser.getUserId());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setDelFlag(DelEnum.NOT_DEL.val);

        user.setPassword(passwordEncoder.encode(req.getPassword()));

        try {
            boolean save = save(user);
            if (save) {
                return R.ok().data(true);
            }
        } catch (Exception e) {
            log.warn("新增用户异常:{}", e.getMessage());
        }
        return R.fail(CodeEnum.ADD_USER_ERROR).data(false);
    }


    /**
     * 查询用户
     *
     * @param id id
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<SysUser> queryUser(Long id) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getId, id);
        wrapper.eq(SysUser::getDelFlag, DelEnum.NOT_DEL.val);
        SysUser user = getOne(wrapper);
        return R.ok().data(user);
    }

    /**
     * 编辑用户
     *
     * @param currentUser  铜
     * @param req 要求事情
     * @return {@link R}<{@link Boolean}>
     */
    @Override
    public R<Boolean> editUser(CurrentUser currentUser, UserReqVO req) {
        Long id = req.getId();
        SysUser user = getById(id);

        String userName = req.getUserName();
        if (StrUtil.isNotBlank(userName)) {
            user.setUserName(userName);
        }
        String nickName = req.getNickName();
        if (StrUtil.isNotBlank(nickName)) {
            user.setNickName(nickName);
        }
        Integer state = req.getState();
        if (state != null) {
            user.setState(state);
        }
        String email = req.getEmail();
        if (StrUtil.isNotBlank(email)) {
            user.setEmail(email);
        }
        Integer sex = req.getSex();
        if (sex != null) {
            user.setSex(sex);
        }
        String mobile = req.getMobile();
        if (StrUtil.isNotBlank(mobile)) {
            user.setEmail(mobile);
        }
        String avatar = req.getAvatar();
        if (StrUtil.isNotBlank(avatar)) {
            user.setAvatar(avatar);
        }
        try {
            boolean update = updateById(user);
            if (update) {
                return R.ok().data(true);
            }
        } catch (Exception e) {
            log.warn("修改用户失败:{}", e.getMessage());
        }
        return R.fail(CodeEnum.UPDATE_USER_ERROR).data(false);
    }


    /**
     * 用户列表
     *
     * @param req 要求事情
     * @return {@link R}
     */
    @Override
    public R userList(QueryUserListReqVO req) {
        return null;
    }
}
